package com.qingke.business;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.qingke.Command.UserCommand;
import com.qingke.mapper.RecordeMapper;
import com.qingke.memo.MemoConsole;
import com.qingke.memo.MemoDao;
import com.qingke.sqlclass.Recorde;

public class ListCommand  extends UserCommand{

	@Override
	public void executeUserCommand() {
		System.out.println("欢迎进入备忘录查询页面：");
		System.out.println("--------------请输入你所要输入的内容");
		System.out.println("1.all - 查询所有结果");
		System.out.println("2.label - 按标签查询");
		System.out.println("3.time - 按提醒时间查询并降序排列备忘录内容");
		String s = null;
		while(true){
			s = MemoConsole.askUserInput("cmd >");
			if(s.equals("1")){
				all();
			}else if(s.equals("2")){
				label();
			}else if(s.equals("3")){
				time();
			}
//			if(Integer.parseInt(command)>0&&Integer.parseInt(command)<4){
//				System.out.println("请重新输入！");
//			}else{
//				s = command;
//				break;
//			}
		}
	}
	
	public static void all(){
		SqlSession session = MemoDao.mybatisSession();
		System.out.println("查询结果如下：");
		RecordeMapper recordeM = session.getMapper(RecordeMapper.class);
		List<Recorde> list= recordeM.selectAll();
		for (Recorde recorde : list) {
			System.out.println(recorde);
		}
		System.out.println("查询结束！");
	}
	
	public static void label(){
		SqlSession session = MemoDao.mybatisSession();
		String title = MemoConsole.askUserInput("请输入标签 : >");
		RecordeMapper recordM = session.getMapper(RecordeMapper.class);
		List<Recorde> list = recordM.selectTitle(title);
		for (Recorde recorde : list) {
			System.out.println(recorde);
		}
	}
	
	public static void time(){
		SqlSession session = MemoDao.mybatisSession();
		RecordeMapper recordM = session.getMapper(RecordeMapper.class);
		List<Recorde> list =  recordM.selectTime();
		for (Recorde recorde : list) {
			System.out.println(recorde);
		}
		
		
//		String remind_time = MemoConsole.askUserInput("请输入提醒时间(yyyy-mm-dd): >");
//		String buq = remind_time+" "+"00:00:00";
//		
//		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		List<Recorde> listA = recordM.selectAll();
//		Date time = null;
//
//		for (Recorde recorde : listA) {
//			time = recorde.getRemindTime();
//			Date d1;
//			try {
//				d1 = df.parse(buq);//字符串转为日期
//				long diff = d1.getTime() - time.getTime();
//				long days = diff / (1000 * 60 * 60 * 24);
//				
//			} catch (ParseException e) {
//				e.printStackTrace();
//			}
//		}
//		List<Recorde> list = recordM.selectRemindTime(buq);
	}
}
